package com.talos.ums.controller.admin.childuser;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.childuser.vo.UmsChildUserPageReqVO;
import com.talos.ums.controller.admin.childuser.vo.UmsChildUserRespVO;
import com.talos.ums.controller.admin.childuser.vo.UmsChildUserSaveReqVO;
import com.talos.ums.dal.dataobject.childuser.UmsChildUserDO;
import com.talos.ums.service.childuser.UmsChildUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 子账号添加记录表")
@RestController
@RequestMapping("/ums/child-user")
@Validated
public class UmsChildUserController {

    @Resource
    private UmsChildUserService childUserService;

    @PostMapping("/create")
    @Operation(summary = "创建子账号添加记录表")
    @PreAuthorize("@ss.hasPermission('ums:child-user:create')")
    public CommonResult<Long> createChildUser(@Valid @RequestBody UmsChildUserSaveReqVO createReqVO) {
        return success(childUserService.createChildUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新子账号添加记录表")
    @PreAuthorize("@ss.hasPermission('ums:child-user:update')")
    public CommonResult<Boolean> updateChildUser(@Valid @RequestBody UmsChildUserSaveReqVO updateReqVO) {
        childUserService.updateChildUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除子账号添加记录表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:child-user:delete')")
    public CommonResult<Boolean> deleteChildUser(@RequestParam("id") Long id) {
        childUserService.deleteChildUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得子账号添加记录表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:child-user:query')")
    public CommonResult<UmsChildUserRespVO> getChildUser(@RequestParam("id") Long id) {
        UmsChildUserDO childUser = childUserService.getChildUser(id);
        return success(BeanUtils.toBean(childUser, UmsChildUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得子账号添加记录表分页")
    @PreAuthorize("@ss.hasPermission('ums:child-user:query')")
    public CommonResult<PageResult<UmsChildUserRespVO>> getChildUserPage(@Valid UmsChildUserPageReqVO pageReqVO) {
        PageResult<UmsChildUserDO> pageResult = childUserService.getChildUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsChildUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出子账号添加记录表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:child-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChildUserExcel(@Valid UmsChildUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsChildUserDO> list = childUserService.getChildUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "子账号添加记录表.xls", "数据", UmsChildUserRespVO.class,
                        BeanUtils.toBean(list, UmsChildUserRespVO.class));
    }

}
